package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.issuer.HallUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.basics.issuer.HallUploadResponse;
import io.renren.common.utils.DateUtil;
import io.renren.modules.basedata.dao.ServiceHallUploadDao;
import io.renren.modules.basedata.entity.tbl_serviceHall;
import io.renren.modules.basedata.service.ServiceHallUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class ServiceHallUploadServiceImpl implements ServiceHallUploadService {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    ServiceHallUploadDao serviceHallUploadDao;
    @Override
    public String serviceHallUpload() throws Exception {
        log.info("开始获取服务网点信息");
        int operResult = 0;
        List<tbl_serviceHall> list = null;
        tbl_serviceHall serviceHall = null;
        list = this.getServiceHallList();
        if (list != null && list.size() > 0) {
            for(int i = 0; i < list.size(); ++i) {
                serviceHall = (tbl_serviceHall)list.get(i);
                HallUploadRequest request = new HallUploadRequest();
                request.setId(serviceHall.getId());
                request.setName(serviceHall.getName());
                request.setTel(serviceHall.getTel());
                request.setContact(serviceHall.getContact());
                request.setServiceItems(serviceHall.getServiceItems());
                request.setAddress(serviceHall.getAddress());
                request.setBusinessHours(serviceHall.getBusinessHours());
                request.setStartTime(DateUtil.formatDate(serviceHall.getStartTime()));
                request.setEndTime(DateUtil.formatDate(serviceHall.getEndTime()));
                request.setOperation(serviceHall.getFlag());
                String fileName = ETCCommHelper.getFileName(request, "65");

                try {
                    HallUploadResponse response = (HallUploadResponse)ETCCommHelper.sdtsUpload(fileName, request, HallUploadResponse.class);
                    log.info("上传服务网点信息服务器返回:" + response.toJson());
                    if (response != null && response.getInfo().equals("成功！")) {
                        if (serviceHallUploadDao.updateServiceHallUploadflag(serviceHall.getId()) == 1) {
                            log.info("服务网点信息表上传标志更新成功！");
                            operResult = 1;
                        } else {
                            log.info("服务网点信息表上传标志更新失败！");
                        }
                    } else {
                        log.info("上传服务网点信息失败!失败原因:" + response.getInfo());
                        serviceHallUploadDao.updateServiceHallUploadflagError(serviceHall.getId());
                    }
                } catch (IOException var9) {
                    log.error("上传服务网点信息失败！错误信息:" + var9.getMessage());
                    var9.printStackTrace();
                } catch (ApiRequestException var10) {
                    log.error("上传服务网点信息失败！错误信息:" + var10.getMessage());
                    if (var10.getErrCode() >= 700 && var10.getErrCode() <= 800) {
                        if (var10.getErrCode() == 707) {
                            serviceHallUploadDao.updateServiceHallUploadflag(serviceHall.getId());
                        } else {
                            serviceHallUploadDao.updateServiceHallUploadflagError(serviceHall.getId());
                        }
                    }

                    var10.printStackTrace();
                }
            }

            return "服务网点信息上传结束！";
        } else {
            return "无服务网点信息,上传结束...";
        }
    }

    public List<tbl_serviceHall> getServiceHallList() throws IOException {
        try {
            List<tbl_serviceHall> list = serviceHallUploadDao.getServiceHallList();
            return list;
        } catch (Exception var3) {
            var3.printStackTrace();
            log.error("获取服务网点信息失败！失败信息:", var3);
            throw new IOException("获取服务网点信息失败:" + var3.getMessage());
        }
    }
}
